package com.itheima.config;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;

/**
 * @author by itheima
 * @Date 2022/4/27
 * @Description 定义servlet容器初始化后调用的配置类，该类无需加载到IOC容器
 *  tomcat----》ServletContainerConfig----》实现WebApplicationInitializer
 */
public class ServletContainerInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {

    /**
     * 定义父容器配置类
     *  父容器主要加载service、dao、aop等相关的bean资源
     * @return
     */
    @Override
    protected Class<?>[] getRootConfigClasses() {
        //直接指定配置类，则配置类的@Configuration注解可省略
        return new Class[]{SpringConfig.class};
    }

    /**
     * 指定配置子容器的配置类：SpringMvcConfig
     * 说白了就是指定加载web层资源的配置类
     * @return
     */
    @Override
    protected Class<?>[] getServletConfigClasses() {
        //直接指定配置类，则配置类的@Configuration注解可省略
        return new Class[]{SpringMvcCofnig.class};
    }

    /**
     * 定义springmvc内置的核心调度器DispatcherServlet的拦截规则
     * @return
     */
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    //中文乱码过滤器
    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
        encodingFilter.setEncoding("utf-8");
        return new Filter[]{encodingFilter};
    }
}
